package com.yxq.mdt.app.modules.message.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 消息
 */
public class MessageTabActivity extends BaseActivity {


    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    String[] tabTitles = new String[]{"打招呼", "私信", "通知", "客服"};
    int[] tabImages = new int[]{R.mipmap.icon_news_say_hello, R.mipmap.icon_news_private_letter, R.mipmap.icon_news_notice, R.mipmap.icon_news_customer_service};
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    List<Fragment> fragmentList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_massage;
    }

    @Override
    public void initView() {
        initTabView();
        initFragment();
        initViewPager();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    private void initTabView() {
        FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return tabTitles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(mActivity);
                commonPagerTitleView.setContentView(R.layout.layout_magicindicator_message_item_layout);

                // 初始化
                final ImageView titleImages = commonPagerTitleView.findViewById(R.id.indicator_tv_title);
                titleImages.setImageResource(tabImages[index]);
                final TextView titleText = commonPagerTitleView.findViewById(R.id.indicator_tv_sub);
                titleText.setText(tabTitles[index]);

                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {

                    @Override
                    public void onSelected(int index, int totalCount) {
                        titleText.setTextColor(getResources().getColor(R.color.color_font_red));
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        titleText.setTextColor(getResources().getColor(R.color.color_font_black));
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                    }
                });

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                        mFragmentContainerHelper.handlePageSelected(index);
                    }
                });


                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {

                return null;
            }
        });
        commonNavigator.setAdjustMode(true);
        magicIndicator.setNavigator(commonNavigator);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
    }

    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new MessageGreetFragment());
        fragmentList.add(new MessagePrivateLetterFragment());
        fragmentList.add(new MessageNoticeFragment());
        fragmentList.add(new MessageCustomerServiceFragment());
    }

    private void initViewPager() {
        TabPagerAdapter mPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(mPagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(getIntent().getIntExtra("tab", 0), false);
    }

    public class TabPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments;

        public TabPagerAdapter(FragmentManager fm, List<Fragment> dataList) {
            super(fm);
            this.mFragments = dataList;

        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }

    }

}
